﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using Janus.Cores.Tools;

namespace Janus.Mes.WebClient.Areas.BaseData.Controllers.Assets
{
    public partial class AssetsController : BaseController
    {
        #region 列表
        /// <summary>
        /// 首页（返回数据列表）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            //return View(GetList(new FormCollection()));
            return View(new List<AssetsVO>());
        }

        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        #endregion

        #region 详情
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Details(string id)
        {
            AssetsVO assets = null;
            if (!string.IsNullOrEmpty(id))
            {
                Querier query = new Querier(" AssetsCode={@0}", new List<object>() { id });
                var model = AssetsServices.FindOne(query);
                assets = model.ToEntity<AssetsVO>();
            }
            else
            {
                assets = base.InitModel<AssetsVO>(assets);
            }
            return View(assets);
        }
        #endregion

        #region 私有方法
        protected IEnumerable<AssetsVO> GetList(FormCollection collection)
        {
            var searchParma = base.SearchPara<AssetsDTO>(collection);
            if (searchParma.Model.AssetsName.IsNotEmpty())
            {
                searchParma.Queriers.Where("AssetsCode.Contains({@0})", searchParma.Model.AssetsCode);
            }
            if (searchParma.Model.AssetsName.IsNotEmpty())
            {
                searchParma.Queriers.Where("AssetsName.Contains({@0})", searchParma.Model.AssetsName);
            }
            //可以继续判断 增加查询条件

            var result = base.FindAll<AssetsDTO>(searchParma, AssetsServices);
            if (!result.ErrorMessage.IsNotEmpty())
            {
                var list = result.Result.ToEntitys<AssetsVO>();
                return list;
            }
            else
            {
                return new List<AssetsVO>();
            }
        }
        #endregion
    }

}